package com.example.communitypro.controller;


import com.example.communitypro.CommunityproApplication;
import com.example.communitypro.pojo.CommunityInformation;
import com.example.communitypro.service.CommunityInformationService;
import com.example.communitypro.service.HomeMessageService;
import com.example.communitypro.util.DateUtil;
import com.example.communitypro.util.result.Result;
import org.json.JSONException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Api(tags = "社区相关接口")
@RequestMapping("/communityInformation")
public class CommunityController {

    @Autowired
    private CommunityInformationService communityInformationService;
    @Autowired
    private HomeMessageService homeMessageService;


    //图片上传测试
    @RequestMapping("/upload")
    @CrossOrigin
    public Map insertCommunityInfo(String option,MultipartFile file, CommunityInformation communityInformation, HttpServletRequest request){

      /*  String communityMember = request.getParameter("communityMember");
        String communityName = request.getParameter("communityName");*/

        String prefix="";
        String dateStr="";
        String property = System.getProperty("user.dir");
        //保存上传
        OutputStream out = null;
        InputStream fileInput=null;
        try{
            if(file!=null){
                String originalName = file.getOriginalFilename();
                prefix=originalName.substring(originalName.lastIndexOf(".")+1);
                Date date = new Date();
                String uuid = UUID.randomUUID()+"";
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                dateStr = simpleDateFormat.format(date);
                String filename =dateStr+"/"+uuid+"." + prefix;
                String filepath = "";
                if(CommunityproApplication.isWindows) {
                    filepath = "C:\\images\\" + dateStr + "\\" + uuid + "." + prefix;
                }
                else
                    filepath = "/home/atipiu/images/" + dateStr + "/" + uuid + "." + prefix;

                communityInformation.setCommunityLcon("http://39.105.106.111:8080/community/image/"+filename);

                File files=new File(filepath);
                //打印查看上传路径
                System.out.println(filepath);
                if(!files.getParentFile().exists()){
                    files.getParentFile().mkdirs();
                }
                file.transferTo(files);
                Map<String,Object> map2=new HashMap<>();
                Map<String,Object> map=new HashMap<>();
                map.put("code",0);
                map.put("msg","success");
                map.put("data",map2);
                map2.put("src","http://106.53.84.209:8080/community-2.5.0/image/" + filename);

                if (!"update".equals(option)){
                    communityInformationService.addCommunityInformation(communityInformation);
                    homeMessageService.add(communityInformation);
                }else {
                    communityInformationService.modifyCommunityInformation(communityInformation);
                }
                return map;
            }

        }catch (Exception e){
        }finally{
            try {
                if(out!=null){
                    out.close();
                }
                if(fileInput!=null){
                    fileInput.close();
                }
            } catch (IOException e) {
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("code",1);
        map.put("msg","filed");



        return map;

    }

    /*@PostMapping("/getVipOrderZt/{user_id}")
    @ApiOperation("查看用户的会员订单支付状态")
    public Result setVip(int user_id) {
        return vipOrderService.getVipOrderZt(user_id);
    }
*/
    @GetMapping("/listCommunity")
    @ApiOperation("通过社区no或者name查询社区")
    @CrossOrigin
    public Result searchCommunity(String communityNo,String communityName) throws JSONException {

        int temp = -1;
        if (!communityNo.equals("")){
            temp = Integer.parseInt(communityNo);
        }

        if (communityNo.equals("") && communityName.equals("")){
            return communityInformationService.findCommunityInformationAll();
        }else {
            Result communityInformationDynamic = communityInformationService.findCommunityInformationDynamic(temp, communityName);
           /* List<CommunityInformation> communityInformationAll = (List<CommunityInformation>) communityInformationDynamic.getData().get("items");

            for (CommunityInformation communityInformation : communityInformationAll) {
                Date date = DateUtil.DateToDate(communityInformation.getCommunityCreationTime());
                communityInformation.setCommunityCreationTime(date);
            }*/
            return communityInformationDynamic;
        }

    }

    @GetMapping("/listCommunityAll")
    @ApiOperation("查询所有社区")
    @CrossOrigin
    public Result searchCommunityAll() {
        return communityInformationService.findCommunityInformationAll();
    }

    @GetMapping("/updateCommunity")
    @ApiOperation("修改指定社区")
    @CrossOrigin
    public Result updateCommunity(CommunityInformation communityInformation) {
        return communityInformationService.modifyCommunityInformation(communityInformation);
    }

    @GetMapping("/deleteCommunity")
    @ApiOperation("删除指定社区")
    @CrossOrigin
    public Result deleteCommunity(String communityNo) {
        return communityInformationService.dropCommunityInformation(Integer.parseInt(communityNo));
    }

}